package com.swordxu.yim.android.imlib;


import com.alibaba.fastjson.JSON;
import com.swordxu.yim.android.config.ProtocolConstant;
import com.swordxu.yim.android.imlib.network.packet.base.DataBuffer;
import com.swordxu.yim.android.imlib.network.packet.base.Header;
import com.swordxu.yim.android.log.Logger;

import io.netty.channel.Channel;

/**
 * <p>消息解析分发类</p>
 * <p>根据解析消息头,处理不同的业务分类.</p>
 * @author 徐晓明
 */
public class IMPacketDispatcher {
	private static Logger logger = Logger.getLogger(IMPacketDispatcher.class);

    /**
     * 静态构造函数
     * @param buffer 服务端发送的消息数据
     * @param channel 连接通道
     */
    public static void dispatch(String buffer, Channel channel) {
        String result = null;
        if (buffer == null) {
            return ;
        }

        //解析消息头
        Header header = new Header();
        System.out.println(JSON.parseObject(buffer).get("header"));
        header = JSON.parseObject(JSON.parseObject(buffer).get("header").toString(), Header.class);
        int sid = header.getSid();
        int cid = header.getCid();

        //判断业务类型
        //登录相关业务
        if (sid == ProtocolConstant.SID_LOGIN) {
            switch(cid) {
                case ProtocolConstant.CID_LOGIN_RES_MSGSERVER:
                    IMLoginManager.instance(channel).onRepMsgServerAddrs(buffer);
                    break;
                case ProtocolConstant.CID_LOGIN_RES_USERLOGIN:
                    IMLoginManager.instance(channel).onRepMsgServerLogin(buffer);
                    break;
            }
        }
        //联系人、部门相关业务
        if (sid == ProtocolConstant.SID_BUDDY_LIST) {
            switch(cid) {
                case ProtocolConstant.CID_BUDDY_LIST_DEPARTMENT_RESPONSE:
                    IMContactManager.instance(channel).onRepDepartment(buffer);
                    break;
                case ProtocolConstant.CID_BUDDY_LIST_ALL_USER_RESPONSE:
                    IMContactManager.instance(channel).onRepAllUsers(buffer);
                    break;
            }
        }
    }

	public static void dispatch(DataBuffer channelBuffer) {
		if (channelBuffer == null) {
			logger.e("packet#channelBuffer is null");
			return;
		}

//		DataBuffer buffer = new DataBuffer(channelBuffer);
//
//		Header header = new Header();
//		header.decode(buffer);
//
//		buffer.resetReaderIndex();
//		int commandId = header.getCommandId();
//		int serviceId = header.getServiceId();
//
//		ProtocolConstant.ProtocolDumper.dump(false, header);
//
//		logger.d("dispatch packet, serviceId:%d, commandId:%d", serviceId,
//				commandId);
//
//		if (serviceId == ProtocolConstant.SID_LOGIN) {
//			switch (commandId) {
//			case ProtocolConstant.CID_LOGIN_RES_MSGSERVER:
//				IMLoginManager.instance().onRepMsgServerAddrs(buffer);
//				return;
//			case ProtocolConstant.CID_LOGIN_RES_USERLOGIN:
//				IMLoginManager.instance().onRepMsgServerLogin(buffer);
//				return;
//			}
//		} else if (serviceId == ProtocolConstant.SID_BUDDY_LIST) {
//			switch (commandId) {
//			case ProtocolConstant.CID_BUDDY_LIST_DEPARTMENT_RESPONSE:
//				IMContactManager.instance().onRepDepartment(buffer);
//				return;
//			case ProtocolConstant.CID_BUDDY_LIST_ALL_USER_RESPONSE:
//				IMContactManager.instance().onRepAllUsers(buffer);
//				return;
//			case ProtocolConstant.CID_BUDDY_LIST_FRIEND_LIST:
//				IMContactManager.instance().onRepRecentContacts(buffer);
//				return;
//			}
//
//		} else if (serviceId == ProtocolConstant.SID_MSG) {
//			switch (commandId) {
//			case ProtocolConstant.CID_MSG_DATA_ACK:
//				IMMessageManager.instance().onMessageAck(buffer);
//				return;
//			case ProtocolConstant.CID_MSG_DATA:
//				IMMessageManager.instance().onRecvMessage(buffer);
//				return;
//			case ProtocolConstant.CID_MSG_UNREAD_CNT_RESPONSE:
//				IMContactManager.instance().onRepUnreadMsgContactList(buffer);
//				return;
//			case ProtocolConstant.CID_MSG_UNREAD_MSG_RESPONSE:
//				IMMessageManager.instance().onRepUnreadMsg(buffer);
//				return;
//			}
//
//		} else if (serviceId == ProtocolConstant.SID_GROUP) {
//			switch (commandId) {
//			case ProtocolConstant.CID_GROUP_LIST_RESPONSE:
//				IMGroupManager.instance().onRepGroupList(buffer);
//				return;
//			case ProtocolConstant.CID_GROUP_DIALOG_LIST_RESPONSE:
//				IMGroupManager.instance().onRepGroupList(buffer);
//				return;
//			case ProtocolConstant.CID_GROUP_CREATE_TMP_GROUP_RESPONSE:
//				IMGroupManager.instance().onRepCreateTempGroup(buffer);
//				return;
//			case ProtocolConstant.CID_GROUP_UNREAD_CNT_RESPONSE:
//				IMGroupManager.instance().onRepUnreadMsgGroupList(buffer);
//				return;
//			case ProtocolConstant.CID_GROUP_UNREAD_MSG_RESPONSE:
//				IMMessageManager.instance().onRepGroupUnreadMsg(buffer);
//				return;
//			case ProtocolConstant.CID_GROUP_CHANGE_MEMBER_RESPONSE:
//				IMGroupManager.instance().onRepChangeTempGroupMembers(buffer);
//				return;
//
//			}
//
//		}
//
//		logger.e("packet#unhandled serviceId:%d, commandId:%d", serviceId,
//				commandId);

	}

}
